/*
 * @Author: your name
 * @Date: 2020-11-14 08:37:28
 * @LastEditTime: 2021-04-05 09:15:00
 * @LastEditors: your name
 * @Description: In User Settings Edit
 * @FilePath: \HDZP\HDY-Perfect\src\store\actionCreators.js
 */
import {
  GET_ADDRESS,
  UPDATE_DEFAULT_ADDRESS,
  SELECTED_ADDRESS,
  UPDATE_APP_CONFIG,
  UPDATE_AVATAR,
  UPDATE_HOME_NOTICE_BAR,
  UPDATE_SUBMIT_CONFIG,
  PUT_ORDER,
  PUSH_ORDER
} from './constants'

import { getAddress } from '@api/profile'

export const updateSubmitConfig = data => ({
  // 提交订单时 选择的商品 规格信息 缓存
  type:UPDATE_SUBMIT_CONFIG,
  data
})

export const updateHomeNoticeBarAction = status => ({
  // 更新首页通知bar 的开关
  type: UPDATE_HOME_NOTICE_BAR,
  status
})

export const updateavatarAction = data => ({
  type: UPDATE_AVATAR,
  data
})

export const updateDefaultAddress = data => ({
  type: UPDATE_DEFAULT_ADDRESS,
  data
})

export const selectedAddressAction = data => ({
  type: SELECTED_ADDRESS,
  data
})


export const getAddressAction = data => {
  return {
    type: GET_ADDRESS,
    data
  }
}

export const updateAppConfigAction = data => {
  console.log(data)
  return {
    type: UPDATE_APP_CONFIG,
    data
  }
}

export const fetchAddressAction = async (dispatch) => {
  const res = await getAddress();
  let temp = []
  temp = res
  let defaultAddress = (temp && temp.length > 0) && temp.find(item => item.is_default === '1')
  if (defaultAddress) {
    dispatch(updateDefaultAddress(defaultAddress))
  } else {
    console.log('没有默认地址', defaultAddress)
    dispatch(updateDefaultAddress({}))
  }

  dispatch(getAddressAction(res));
}

// get order
export const getOrder = data => {
  return {
    type: PUT_ORDER,
    data
  }
}

//  clean、save、update order
export const updateOrder = data => {
  return{
    type: PUSH_ORDER,
    data
  }
}

